package org.hxyjs.service.impl;

import org.hxyjs.dao.SupplierorderDao;
import org.hxyjs.dto.DTO;
import org.hxyjs.dto.MessageEnum;
import org.hxyjs.entity.Supplierorder;
import org.hxyjs.page.PageInfo;
import org.hxyjs.service.SupplierorderService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Service("SupplierorderService")
public class SupplierorderServiceImpl implements SupplierorderService {
    @Resource
    SupplierorderDao supplierorderDao;
    @Override
    public DTO<Supplierorder> getAllSupplierorders()  throws Exception{
        List<Supplierorder> supplierorders = supplierorderDao.getAllSupplierorders();
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(supplierorders).isPresent()?MessageEnum.GETADMINSUCCESS:MessageEnum.GETADMINFAIL;
        return new DTO<Supplierorder>(messageEnum.getCode(),messageEnum.getMessage(),supplierorders);
    }

    @Override
    public DTO<Supplierorder> deleteSupplierorderByUsno(String usno)  throws Exception{
        Integer result = supplierorderDao.deleteSupplierorderByUsno(usno);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  =result> 0?MessageEnum.DELETEADMINSUCCESS:MessageEnum.DELETEADMINFAIL;
        return new DTO<Supplierorder>(messageEnum.getCode(),messageEnum.getMessage());
    }
    @Override
    public DTO<Supplierorder> getSupplierorderByUsno(String usno)  throws Exception{
        Supplierorder supplierorders = supplierorderDao.getSupplierorderByUsno(usno);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(supplierorders).isPresent()?MessageEnum.GETADMINSUCCESS:MessageEnum.GETADMINFAIL;
        return new DTO<Supplierorder>(messageEnum.getCode(),messageEnum.getMessage(),supplierorders);
    }

    @Override
    public DTO<Supplierorder> updateSupplierorderByUsno(Supplierorder supplierorder) throws Exception {
        Integer result = supplierorderDao.updateSupplierorderByUsno(supplierorder);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  =result> 0?MessageEnum.UPDATEADMINSUCCESS:MessageEnum.UPDATEADMINFAIL;
        return new DTO<Supplierorder>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<Supplierorder> addSupplierorder(Supplierorder supplierorder) throws Exception {
        Integer result = supplierorderDao.addSupplierorder(supplierorder);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  =result> 0?MessageEnum.ADDADMINSUCCESS:MessageEnum.ADDADMINFAIL;
        return new DTO<Supplierorder>(messageEnum.getCode(),messageEnum.getMessage());
    }

    /**
     * 学生选课成功 学生成绩加1分，从选课信息中可以获得学生的学号就能查询出学生原来的成绩
     * @param
     * @return
     * @throws Exception
     */
    @Override
    public DTO<Supplierorder> getSupplierorderByPage(PageInfo pageInfo) throws Exception {
        List<Supplierorder> supplierorders = supplierorderDao.getSupplierorderByPage(pageInfo);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(supplierorders).isPresent()?MessageEnum.GETADMINSUCCESS:MessageEnum.GETADMINFAIL;
        return new DTO<Supplierorder>(messageEnum.getCode(),messageEnum.getMessage(),supplierorders,pageInfo);
    }
    @Cacheable(value = "myCache",key = "#cacheId")
    @Override
    public DTO<Supplierorder> getSupplierorderByPage(PageInfo pageInfo, String cacheId) throws Exception {
        List<Supplierorder> supplierorders = supplierorderDao.getSupplierorderByPage(pageInfo);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(supplierorders).isPresent()?MessageEnum.GETADMINSUCCESS:MessageEnum.GETADMINFAIL;
        return new DTO<Supplierorder>(messageEnum.getCode(),messageEnum.getMessage(),supplierorders,pageInfo);
    }

}
